package test_achieve.h5_2;

import constant.Constant;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.api.java.zh_cn.那么;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import util.tools.CommonTools;

import java.text.DecimalFormat;
import java.util.concurrent.TimeUnit;

/**
 * H5端下单业务相关代码实现层
 */
public class OrderMain {
    @Given("^H未登录进入下单页面$")
    public void H未登录进入下单页面() throws Throwable {
//        System.setProperty("webdriver.firefox.bin", "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
//        Constant.driverWeb = new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
        Constant.driverH = new ChromeDriver();
        Constant.driverH.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        Constant.driverH.get(Constant.h5MakeOrderURL);
    }

    @Given("^H点击发件信息$")
    public void H点击发件信息() throws Throwable {
        CommonTools.scrollToElement(Constant.driverH,
                Constant.driverH.findElements(By.cssSelector("div.i-address-mess")).get(0));
        Constant.driverH.findElements(By.cssSelector("div.i-address-mess")).get(0).click();
    }

    @Given("^H点击选择城市图标$")
    public void H点击选择城市图标() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div/div[1]/div[1]/div[2]/img[2]")).click();
    }

    @Given("^H选择城市:(.+)$")
    public void H选择城市(String city) throws Throwable {
        Thread.sleep(2000);
        int count = Constant.driverH.findElements(By.xpath("//*[text()=\"" + city + "\"]")).size();
        if ("北京市".equals(city) && count == 2) {
            CommonTools.scrollToElement(Constant.driverH, Constant.driverH.findElements(By.xpath("//*[text()=\"" + city + "\"]")).get(1));
            Constant.driverH.findElements(By.xpath("//*[text()=\"" + city + "\"]")).get(1).click();

        } else {
//            CommonTools.scrollToElement(Constant.driverH, Constant.driverH.findElement(By.xpath("//*[text()=\"" + city + "\"]")));
            CommonTools.scrollToElement(Constant.driverH,By.xpath("//*[text()=\"" + city + "\"]"));

            Constant.driverH.findElement(By.xpath("//*[text()=\"" + city + "\"]")).click();
        }

    }

    @Given("^H设置发件地址信息:(.+)$")
    public void H设置发件地址信息(String fAdr) throws Throwable {
        //输入寄件地址
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[1]/div/input")).sendKeys(fAdr);
//        Constant.driverH.findElement(
//                By.cssSelector("input[class='input inputAddress']")).sendKeys(fAdr);
        Thread.sleep(4000);
        //选择第一个
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/div/div[2]/ul/li[1]/p[1]")).click();
    }

    @Given("^H设置收件地址信息:(.+)$")
    public void H设置收件地址信息(String fAdr) throws Throwable {
        //输入寄件地址
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[1]/div/input")).sendKeys(fAdr);
//        Constant.driverH.findElement(
//                By.cssSelector("input[class='input inputAddress']")).sendKeys(fAdr);
        Thread.sleep(4000);
        //选择第一个
        Constant.driverH.findElement(
                By.cssSelector("p.i-listop")).click();
    }


    @Given("^H设置收寄件人信息:(.+)$")
    public void H设置收寄件人信息(String tel) throws Throwable {
//        PubTools.waitForPageLoadByXpath(Constant.driverH,"//*[@id=\"iss-app\"]/div/section/div[1]/div[3]/div[1]/input");
//        Thread.sleep(1000);
        //输入收寄件人姓名
        if (CommonTools.isElementExist(Constant.driverH, By.cssSelector("input[placeholder='发件人姓名']"))) {
            Constant.driverH.findElement(
                    By.cssSelector("input[placeholder='发件人姓名']")).sendKeys("Benchi");
            //输入寄件人手机
            Constant.driverH.findElement(
                    By.cssSelector("input[placeholder='发件人电话']")).sendKeys(tel);
        } else {
            Constant.driverH.findElement(
                    By.cssSelector("input[placeholder='收件人姓名']")).sendKeys("Benchi");
            //输入收件人手机
            Constant.driverH.findElement(
                    By.cssSelector("input[placeholder='收件人电话']")).sendKeys(tel);
        }


    }

    @Given("^H点击送到哪里$")
    public void H点击送到哪里() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"company-app\"]/div/div/div/div[1]/div[2]/div/div[2]/div[2]/span")).click();
    }

    @And("^H不使用优惠劵$")
    public void H不使用优惠劵() throws Throwable {
        Thread.sleep(1000);
        WebElement e = Constant.driverH.findElement(By.cssSelector("span[class='i-r i-coupon-show']"));
        if ("使用优惠券".equals(e.getText())) {
            return;
        }
        e.click();
        Constant.driverH.findElement(By.cssSelector("div[class='c-list-info']")).click();
    }

    @And("^H点击优惠劵$")
    public void H点击优惠劵() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(By.xpath("//*[text()=\"优惠券\"]")).click();
    }

    @And("^H选择优惠劵：(.+)$")
    public void H选择优惠劵(String coupon) throws Throwable {
        Thread.sleep(1000);
        int index=CommonTools.index(Constant.driverH,By.cssSelector("p.c-name"),coupon);
        //如果 优惠劵已经被选择了，则return
        if (CommonTools.isElementExist(Constant.driverH, By.cssSelector("img.my-coupon"))) {
            if (coupon.equals(Constant.driverH.findElement(By.cssSelector("img.my-coupon + p.c-name")).getText())) {
                Constant.driverH.navigate().back();
              return;
            }else  if ( Constant.driverH.findElements(By.cssSelector("p.c-time.c-sub")).get(index).getText().contains("不可用")){
                Constant.driverH.navigate().back();
                return;
            }else {
                Constant.driverH.findElement(By.xpath("//*[text()=\""+coupon+"\"]")).click();
            }
        }else  if ( Constant.driverH.findElements(By.cssSelector("p.c-time.c-sub")).get(index).getText().contains("不可用")){
            Constant.driverH.navigate().back();
            return;
        }else {
            Constant.driverH.findElement(By.xpath("//*[text()=\""+coupon+"\"]")).click();
        }

    }


    @Then("^H优惠金额正确：(.+)$")
    public void H优惠金额正确(String coupon) throws Throwable {
        Thread.sleep(1000);
        String a = Constant.driverH.findElement(By.cssSelector("span.i-r.i-coupon-show")).getText();
        String e = "优惠" + coupon + "元";
        org.testng.Assert.assertEquals(a, e);
    }

    @Given("^H点击提交订单$")
    public void H点击提交订单() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(By.cssSelector("div.i-sub-btn")).click();
    }

    @Then("^H提交订单成功$")
    public void H提交订单成功() throws Throwable {
        WebElement e = Constant.driverH.findElement(By.cssSelector("div[class='p-t-con']"));
        Assert.assertTrue(e.getText().contains("支付剩余时间"));
    }

    @Then("^H订单状态正确:(.+)$")
    public void H订单状态正确(String s) throws Throwable {
        Thread.sleep(1000);
        String status = Constant.driverH.findElement(By.cssSelector("div.order-f")).getText();
//       System.out.println("订单状态："+status+"   s:"+s);
        org.testng.Assert.assertEquals(status, s);
    }

    @Then("^H订单详情状态正确:(.+)$")
    public void H订单详情状态正确(String s) throws Throwable {
        Thread.sleep(1000);
        WebElement e = Constant.driverH.findElement(By.cssSelector("p.o-courier"));
//        System.out.println("状态 "+s);
//        System.out.println("状态 "+e.getText());
        org.testng.Assert.assertTrue(e.getText().contains(s));
    }

    @Given("^H点击确定$")
    public void H点击确定() throws Throwable {
        Constant.driverH.findElement(By.linkText("确定")).click();
    }

    @And("^H点击确认物品送达$")
    public void H点击确认物品送达() throws Throwable {
        Constant.driverH.findElement(By.cssSelector("input[value='确认物品送达']")).click();
    }

    @Given("^H填写物品类型:(.+)$")
    public void H填写物品类型(String type) throws Throwable {
        //输入物品类型(无标签类型，等h5标签类型上线后 干掉该代码，用下面注释的代码)
//         Constant.driverH.findElement(
//                By.xpath("//*[@id=\"iss-app\"]/div/div/div[4]/div[1]/input")).sendKeys(type);

        Constant.driverH.findElement(
                By.cssSelector("span.weight-info.goods-type-info")).click();
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/ul/li[3]/span")).click();
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div/button")).click();
        //回到下单页后，下拉滚动条，便于其他元素全部显示出来，为后续操作提供方便  (拉到 ‘我要加价’出现)
        CommonTools.scrollToElement(Constant.driverH, By.cssSelector("span.text-mess"));

    }

    @Given("^H填写备注:(.+)$")
    public void H填写备注(String memo) throws Throwable {
        CommonTools.scrollToElement(Constant.driverH, By.cssSelector("div.i-mark-tit"));
//        WebElement e=Constant.driverH.findElement(By.cssSelector("input.edit-input"));
//        CommonTools.setAttribute(Constant.driverH,e,"value",memo);
//
        Constant.driverH.findElement(By.cssSelector("div.i-mark-tit")).click();
        Constant.driverH.findElement(By.cssSelector("input.edit-input")).sendKeys(memo);
        Constant.driverH.findElement(By.linkText("确认")).click();
    }

    @Given("^H点击下一步$")
    public void H点击下一步() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/button")).click();
    }

    @Given("^H点击支付$")
    public void H点击支付() throws Throwable {
        Thread.sleep(1000);
        CommonTools.waitElementLoad(Constant.driverH, "xpath", "//*[text()=\"支付\"]");
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"支付\"]")).click();
        Thread.sleep(2000);
        if (CommonTools.isElementExist(Constant.driverH, By.cssSelector("a.close.zfgbtc"))) {
            Constant.driverH.findElement(
                    By.cssSelector("img[src='images/main_advertisement_close.png']")).click();
        }

    }

    @Then("^H支付成功$")
    public void H支付成功() throws Throwable {
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("//*[text()=\"订单详情\"]")).getText().equals("订单详情"));

    }

    @Given("^H回到首页$")
    public void H回到首页() throws Throwable {
        Constant.driverH.navigate().refresh();
        while (!CommonTools.isElementExist(Constant.driverH, By.cssSelector("div.i-m-center"))) {
//            System.out.println("====返回首页！！！");
            CommonTools.back(Constant.driverH, 1);
        }
        Thread.sleep(1000);
        CommonTools.scrollToElement(Constant.driverH, By.cssSelector("div.i-m-center"));
    }

    @And("^H点击个人中心图标$")
    public void H点击个人中心图标() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverH, "xpath", "//*[@id=\"company-app\"]/div/div/div/div[1]/div[1]/div[1]/div/img");
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.cssSelector("div.i-m-center")).click();
    }

    @And("^H点击设置中心$")
    public void H点击设置中心() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(By.xpath("//*[text()=\"设置中心\"]")).click();
    }

    @And("^H点击退出登录$")
    public void H点击退出登录() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"退出登录\"]")).click();
    }

    @Then("^H验证退出登录$")
    public void H验证退出登录() throws Throwable {
        Thread.sleep(1000);
        String s = Constant.driverH.findElement(
                By.cssSelector("li.activity.tab-h1")).getText();
        org.testng.Assert.assertEquals("快捷登录", s);
    }

    @When("^H点击交易明细$")
    public void H点击交易明细() throws Throwable {
        Constant.driverH.findElement(
                By.linkText("交易明细")).click();
    }

    @When("^H点击交易明细记录:(.+)$")
    public void H点击交易明细记录(String s) throws Throwable {
        Constant.driverH.findElement(
                By.linkText(s)).click();
    }

    @Then("^H验证闪送余额:(\\d+)$")
    public void H验证闪送余额(int p) throws Throwable {
        int account = Integer.parseInt(
                Constant.driverH.findElement(
                        By.xpath("//*[@id=\"subcard-balance\"]/b")).getText());
        Assert.assertTrue(account == (Constant.h5Account - p));
    }

    @Then("^H支付页验证闪送余额$")
    public void H支付页验证闪送余额() throws Throwable {
        Thread.sleep(1000);
        String s = Constant.driverH.findElement(By.cssSelector("span.p-p-p")).getText();
        float account = Float.parseFloat(CommonTools.getNumbers(s));
        org.testng.Assert.assertEquals(account, Constant.h5Account);
    }


    @When("^H点击充值记录$")
    public void H点击充值记录() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"充值记录\"]")).click();
    }

    @Then("^H验证充值记录:(.+)$")
    public void h验证充值记录(String p) throws Throwable {
        Thread.sleep(1000);
        String record = Constant.driverH.findElement(
                By.cssSelector("span.label")).getText();
        record = record.replaceAll("\r|\n|,|\\s*", "");//去换行，逗号，空格
//        System.out.println("充值记录"+record);
        Assert.assertEquals(p, record);
    }

    @When("^H点击退款记录$")
    public void H点击退款记录() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"退款记录\"]")).click();
    }

    @Then("^H验证退款记录:(.+)$")
    public void H验证消费记录h验证退款记录(String p) throws Throwable {
        Thread.sleep(2000);
        String record = Constant.driverH.findElement(
                By.cssSelector("span[data-reactid='.0.1.$1.0']")).getText();
//        System.out.println("退款记录"+record);
        Assert.assertTrue(record.contains(p));
    }

    @And("^H点击我的订单$")
    public void H点击我的订单() throws Throwable {
        Constant.driverH.findElement(
                By.linkText("我的订单")).click();
    }

    @And("^H点击订单详情$")
    public void H点击订单详情() throws Throwable {
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"" + Constant.OrderNumbuter.get(0) + "\"]")).click();
    }

    @And("^H获得订单号$")
    public void H获得订单号() throws Throwable {
        Thread.sleep(2000);
        String orderN = Constant.driverH.findElement(
                By.xpath("//*[text()=\"复制\"]")).getAttribute("value");
        System.out.println("订单号：" + orderN);
        Constant.OrderNumbuter.add(orderN);


    }

    @And("^H订单列表页获得订单号$")
    public void H订单列表页获得订单号() throws Throwable {
        Thread.sleep(1000);
        String n = Constant.driverH.findElement(
                By.cssSelector("div.order-num")).getText();
        Constant.OrderNumbuter.add(n);
//        System.out.println( Constant.OrderNumbuter.get(0));


    }

    @And("^H获得订单号2$")
    public void H获得订单号2() throws Throwable {
        Thread.sleep(3000);
        Constant.driverH.navigate().refresh();
        Constant.OrderNumbuter.add(Constant.driverH.findElement(
                By.xpath("//*[text()=\"复制\"]")).getAttribute("value"));

//        System.out.println("订单号============："+ string);
//        for (int i=0;i<Constant.OrderNumbuter.size();i++){
//            System.out.println("订单号："+ Constant.OrderNumbuter.get(i));
//        }

    }


    @And("^H取消订单$")
    public void H取消订单() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.cssSelector("input[value='取消订单']")).click();
        Thread.sleep(1000);
        //弹出窗口点击 确认取消
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"确认取消\"]")).click();
    }

    @And("^H点击去支付$")
    public void H点击去支付() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.cssSelector("input[value='去支付']")).click();

    }

    @And("^H点击全部$")
    public void H点击全部() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"全部\"]")).click();

    }

    @And("^H点击进行中$")
    public void H点击进行中() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"进行中\"]")).click();

    }

    @And("^H点击完成单$")
    public void H点击完成单() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"完成单\"]")).click();

    }

    @And("^H点击取消单$")
    public void H点击取消单() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"取消单\"]")).click();

    }

    @And("^H点击返回首页$")
    public void H点击返回首页() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.cssSelector("input[value='返回首页']")).click();
    }

    @And("^H点击我的钱包$")
    public void H点击我的钱包() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverH, "linkText", "我的钱包");
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.linkText("我的钱包")).click();
    }

    @And("^H点击我的优惠券$")
    public void H点击我的优惠券() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverH, "linkText", "我的优惠券");
        Thread.sleep(1000);
        Constant.driverH.findElement(
                By.linkText("我的优惠券")).click();
    }

    @And("^H获取闪送余额$")
    public void H获取闪送余额() throws Throwable {
        CommonTools.waitElementLoad(Constant.driverH, "xpath", "//*[@id=\"company-app\"]/div/div/div/p[1]");
        Thread.sleep(2000);
        String money = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div/p[2]")).getText();
        Constant.h5Account = Float.parseFloat(
                money.substring(0, money.length() - 1)
        );
        System.out.println("h获取闪送余额:" + Constant.h5Account);
    }

    @And("^H回退上一页:(\\d+)$")
    public void H回退上一页(int i) throws Throwable {
        CommonTools.back(Constant.driverH, i);
    }

    @And("^H点击订单金额详情$")
    public void H点击订单金额详情() throws Throwable {
//         Constant.driverH.findElement(
//                By.xpath("//*[@id=\"company-app\"]/div/div/div/div[3]/div[1]/div[2]")).click();
        Constant.driverH.findElement(
                By.xpath("//*[text()=\"价格明细\"]")).click();
    }

    @And("^H点击我要加价$")
    public void H点击我要加价() throws Throwable {

        // 滚动到元素可见的位置
        CommonTools.scrollToElement(Constant.driverH,
                By.cssSelector("span.text-mess"));

        Constant.driverH.findElements(
                By.cssSelector("span.text-mess")).get(1).click();
    }

    @Given("^H点击取件时间$")
    public void H点击取件时间() throws Throwable {
        Constant.driverH.findElement(By.cssSelector("span.fetch-time-input")).click();
    }


    @And("^H进入订单详情$")
    public void H进入订单详情() throws Throwable {
        Thread.sleep(1000);
        String orderN = Constant.OrderNumbuter.get(Constant.OrderNumbuter.size() - 1);
        Constant.driverH.findElement(By.xpath("//*[text()=\"" + orderN + "\"]")).click();
    }


    @And("^H设置加小费重推金额:(\\d+)$")
    public void H设置加小费重推金额(int addPrice) throws Throwable {
        Thread.sleep(1000);
        WebElement element = Constant.driverH.findElement(By.cssSelector("input[value='加小费']"));
        CommonTools.scrollToElement(Constant.driverH, element);
        element.click();
        //-----处理加消费的逻辑
        Actions action = new Actions(Constant.driverH);
        Thread.sleep(1000);
        //进来后，默认的0 顶层位置
        WebElement s = Constant.driverH.findElement(By.cssSelector("div.zby-picker-column-indicator"));
        //要加价的钱数

        int n = addPrice / 5;
        for (int i = 0; i < n; ) {
            WebElement e = Constant.driverH.findElement
                    (By.xpath("//*[@id=\"company-app\"]/div/div/div[3]/div[3]/div[1]/div[2]/div[2]/div/div/div/div[3]/div[" + n + "]"));
            action.clickAndHold(s).moveToElement(e).perform();
            action.click(s).perform();
//            Thread.sleep(5000);
            i = i + 2;
        }
        Constant.driverH.findElement(By.xpath("//*[text()=\"确定\"]")).click();


    }

    @Then("^H验证加价金额:(\\d+)$")
    public void H验证加价金额(int addPrice) throws Throwable {
        Assert.assertTrue(
                Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[1]/div/div[2]/span[2]")).getText().contains(addPrice + "")
        );
        Assert.assertTrue(
                Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[3]/div/div")).getText().contains(addPrice + "")
        );
    }

    @Then("^H验证里程费:(\\d+)$")
    public void H验证里程费(int p) throws Throwable {
        String price = Constant.driverH.findElement(
                By.cssSelector("span.fr")).getText();
        Assert.assertEquals(p + "元", price);

    }

    @Then("^H验证已优惠:(\\d+)$")
    public void H验证已优惠(int p) throws Throwable {
        String price = Constant.driverH.findElements(By.cssSelector("span.fr")).get(1).getText();
        Assert.assertEquals("-" + p + "元", price);
    }

    @Then("^H验证订单总额:(\\d+)$")
    public void H验证订单总额(int p) throws Throwable {
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("/html/body/div[2]/div[1]/ul[2]/li[1]/span[2]")).getText().equals(p + "元")
        );

    }

    @Then("^H验证您需支付的金额:(\\d+)$")
    public void H验证您需支付的金额(int p) throws Throwable {
        Assert.assertTrue(
                Constant.driverH.findElement(
                        By.xpath("/html/body/div[2]/div[1]/ul[2]/li[2]/span[2]")).getText().equals(p + "元")
        );
    }

    @Then("^H验证续重费:(\\d+)$")
    public void H验证续重费(int p) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("span.fr")).get(1).getText();
//        System.out.println("续重费"+s);
        Assert.assertEquals(p + "元", s);
    }

    @Then("^H验证夜间费:(\\d+)$")
    public void H验证夜间费(int p) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("span.fr")).get(2).getText();
//        System.out.println("夜间费"+s);
        Assert.assertEquals(p + "元", s);
    }

    @Then("^H验证加价费:(\\d+)$")
    public void H验证加价费(int p) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("span.fr")).get(3).getText();
//        System.out.println("加价费"+s);
        Assert.assertEquals(p + "元", s);
    }

    @And("^H点击加价重推去支付$")
    public void H点击加价重推去支付() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[3]/button")).click();
    }

    @And("^H填写备注信息:(.+)$")
    public void H填写备注信息(String memo) throws Throwable {
        //再次点击物品类型进入
        Constant.driverH.findElement(
                By.cssSelector("span[class='weight-info goods-type-info']")).click();
        //写入备注信息
        Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div/textarea")).sendKeys(memo);
        //点击 确定
        Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div/button")).click();
        //回到下单页后，下拉滚动条，便于其他元素全部显示出来，为后续操作提供方便  (拉到 ‘我要加价’出现)
        CommonTools.scrollToElement(Constant.driverH, By.cssSelector("span.text-mess"));

    }

    @And("^H点击再来一单$")
    public void H点击再来一单() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElements(By.cssSelector("input.o-common-btn.o-handle")).get(0).click();
    }

    @And("^H点击物品重量$")
    public void H点击物品重量() throws Throwable {
        Thread.sleep(1000);
        Constant.driverH.findElement(By.cssSelector("div[class='i-weight f-blue']")).click();
    }


    @And("^H点击返回:(\\d+)$")
    public void H点击返回(int i) throws Throwable {
        CommonTools.back(Constant.driverH, i);
    }

    @And("^H刷新页面$")
    public void H刷新页面() throws Throwable {
        Constant.driverH.navigate().refresh();
    }

    @And("^H点击订单列表标题:(.+)$")
    public void H点击订单列表标题(String title) throws Throwable {
        Thread.sleep(2000);
        Constant.driverH.findElement(By.linkText(title)).click();
    }

    @And("^H进入订单进度$")
    public void H进入订单进度() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[1]/a[1]/span")).click();
    }

    @And("^H订单详情中点击订单总价详情$")
    public void H订单详情中点击订单总价详情() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[1]/span[3]")).click();
    }

    @Then("^H验证订单详情中里程金额:(.+)$")
    public void H验证订单详情中里程金额(String price) throws Throwable {
        String p = Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[2]/div/span[2]")).getText().trim();
        Assert.assertTrue(p.equals(price));
    }

    @Then("^H验证订单详情中加价费:(.+)$")
    public void H验证订单详情中加价费(String price) throws Throwable {
        String p = Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[2]/div[2]/span[2]")).getText().trim();
        Assert.assertTrue(p.equals(price));
    }

    @Then("^H验证订单详情中续重金额:(.+)$")
    public void H验证订单详情中续重金额(String price) throws Throwable {
        String p = Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[2]/div[2]/span[2]")).getText().trim();
        Assert.assertTrue(p.equals(price));
    }

    @Then("^H验证订单详情中夜间费:(.+)$")
    public void H验证订单详情中夜间费(String price) throws Throwable {
        String p = Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[2]/div[3]/span[2]")).getText().trim();
        Assert.assertTrue(p.equals(price));
    }

    @Then("^H验证订单详情中加价金额:(.+)$")
    public void H验证订单详情中加价金额(String price) throws Throwable {
        String p = Constant.driverH.findElement(
                By.xpath("//*[@id=\"iss-app\"]/div/div[2]/div[2]/div[1]/div[3]/div[5]/div/div[1]/div[2]/div[4]/span[2]")).getText();
        Assert.assertTrue(p.equals(price));
    }

    @When("^H点击关闭图标$")
    public void H点击关闭图标() throws Throwable {
        Constant.driverH.findElement(
                By.cssSelector("div.close-btn")).click();
    }


    @Then("^H下单页验证寄件信息:(.+)：(.+)$")
    public void H下单页验证寄件信息(String inf, String phone) throws Throwable {
        String s = Constant.driverH.findElement(
                By.cssSelector("div.i-adres")).getText();
        s = s.replaceAll("\\s*|\t|\r|\n", "");
        String tel = Constant.driverH.findElement(
                By.cssSelector("span.i-tel")).getText();
        tel = tel.replaceAll("\\s*|\t|\r|\n", "");
        Assert.assertTrue(s.contains(inf));
        Assert.assertTrue(tel.equals(phone));
    }

    @Then("^H下单页验证收件信息:(.+)：(.+)$")
    public void H下单页验证收件信息(String inf, String phone) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("div.i-adres")).get(1).getText();
        s = s.replaceAll("\\s*|\t|\r|\n", "");
        String tel = Constant.driverH.findElements(
                By.cssSelector("span.i-tel")).get(1).getText();
        tel = tel.replaceAll("\\s*|\t|\r|\n", "");
        Assert.assertTrue(s.contains(inf));
        Assert.assertTrue(tel.equals(phone));
    }

    @Then("^H下单页验证物品类型信息:(.+)$")
    public void H下单页验证物品类型信息(String inf) throws Throwable {
        String s = Constant.driverH.findElement(
                By.cssSelector("span.weight-info.goods-type-info")).getText();
        Assert.assertTrue(s.equals(inf));
    }

    @Then("^H下单页验证物品重量信息:(.+)$")
    public void H下单页验证物品重量信息(String inf) throws Throwable {
        String s = Constant.driverH.findElement(
                By.cssSelector("div[class='i-weight f-blue']")).getText();
        if (Integer.parseInt(inf) <= 5) {
            inf = "<5";
        }
//        System.out.println("物品重量信息："+s);
        Assert.assertTrue(s.equals(inf + "公斤"));
    }

    @Then("^H下单页验证取件时间:(.+)：(.+)$")
    public void H下单页验证取件时间(String date, String hour) throws Throwable {
        //1 设置取件时间(今天or明天)
        if (Integer.parseInt(hour) < 10) {
            hour = "0" + hour;
        }
        String mydate = CommonTools.getDate(date);
        String s = Constant.driverH.findElement(
                By.cssSelector("span[class='i-time-show i-r']")).getText();
//        System.out.println("下单页验证取件时间:" + s);
//        System.out.println("下单页验证取件时间:" + mydate + " " + hour);
        Assert.assertTrue(s.contains(mydate + " " + hour + ":00"));
    }

    @Then("^H订单详情页验证寄件信息:(.+)：(.+)$")
    public void H订单详情页验证寄件信息(String inf, String phone) throws Throwable {
        Thread.sleep(1000);
        String s = Constant.driverH.findElement(
                By.cssSelector("div.addr-detail-info.f-gray")).getText();
        s = s.replaceAll("\\s*|\t|\r|\n", "");
        String tel = Constant.driverH.findElement(
                By.cssSelector("span.mobile")).getText();
//        tel = tel.replaceAll("\\s*|\t|\r|\n", "");
//        System.out.println("寄件信息"+s);
        Assert.assertTrue(s.contains(inf));
        Assert.assertTrue(tel.equals(phone));
    }

    @Then("^H订单详情页验证收件信息:(.+)：(.+)$")
    public void H订单详情页验证收件信息(String inf, String phone) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("div.addr-detail-info.f-gray")).get(1).getText();
        s = s.replaceAll("\\s*|\t|\r|\n", "");
        String tel = Constant.driverH.findElements(
                By.cssSelector("span.mobile")).get(1).getText();
//        tel = tel.replaceAll("\\s*|\t|\r|\n", "");
//        System.out.println("寄件信息"+s);
        Assert.assertTrue(s.contains(inf));
        Assert.assertTrue(tel.equals(phone));
    }

    @Then("^H订单详情页验证物品信息:(.+)$")
    public void H订单详情页验证物品信息(String kg) throws Throwable {
        String s = Constant.driverH.findElements(
                By.cssSelector("span[class='o-information']")).get(0).getText();
        s = s.replaceAll("\\s*|\t|\r|\n", "");
        if (Integer.parseInt(kg) <= 5) {
            kg = "5公斤";
        } else {
            kg = kg + "公斤";
        }
//        System.out.println(s);
        Assert.assertTrue(s.equals("文件/" + kg));
    }

    @Then("^H订单详情页验证取件时间:(.+)：(.+)$")
    public void H订单详情页验证取件时间(String date, String hour) throws Throwable {
        //1 设置取件时间(今天or明天)
        String mydate = CommonTools.getDate2(date);
        String s = Constant.driverH.findElements(
                By.cssSelector("span[class='o-information']")).get(1).getText();
        if (Integer.parseInt(hour) < 10) {
            hour = "0" + hour;
        }
//        System.out.println("下单页验证取件时间:"+s);
//        System.out.println("下单页验证取件时间:"+mydate+" "+hour+":00:00");
        Assert.assertTrue(s.contains(mydate + " " + hour + ":00:00"));
    }

    @Then("^H去支付页验证物品类型信息:(.+)$")
    public void H去支付页验证物品类型信息(String inf) throws Throwable {
        String s = Constant.driverH.findElement(
                By.cssSelector("p.icon-item-name.pay-info-item")).getText();
        Assert.assertTrue(s.equals(inf));
    }

    @那么("^H账户余额正确:(\\d+)$")
    public void H账户余额正确(int cost) throws Throwable {
        Thread.sleep(2000);
        //获取当前账户余额
        String money = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div/p[2]")).getText();
        Float accout = Float.parseFloat(money.substring(0, money.length() - 1));
        System.out.println("下完单后账户余额为：" + accout);
        System.out.println("Constant.h5Account-cost 为：" + (Constant.h5Account - cost));
        org.testng.Assert.assertEquals(accout, Constant.h5Account - cost);
    }


    @When("^H点击查看明细$")
    public void H点击查看明细() throws Throwable {
        Constant.driverH.findElement(By.xpath("//*[text()=\"查看明细\"]")).click();
    }

    @Then("^H验证消费记录:(.+)$")
    public void H验证消费记录(String p) throws Throwable {
        String record = Constant.driverH.findElement(
                By.cssSelector("span[data-reactid='.0.1.$0.0']")).getText();
//        System.out.println(record);
        Assert.assertTrue(record.contains(p));
    }

    @Then("^H价格明细正确：(.+)$")
    public void H价格明细正确(String p) throws Throwable {
        String price = Constant.driverH.findElement(
                By.cssSelector("div.o-price.o-title > span.o-information")).getText();
//        System.out.println(price;
        Assert.assertEquals(p + "元", price);
    }

    @Then("^H验证加价重推消费记录：(\\d+)：(\\d+)$")
    public void H验证加价重推消费记录(int addPrice, int totalPrice) throws Throwable {
        Thread.sleep(1000);
        String addP = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/ul/li[1]/span[1]")).getText();
        String totalP = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/ul/li[2]/span[1]")).getText();
//        System.out.println("addP:"+addP);
//        System.out.println("totalP:"+totalP);
        org.testng.Assert.assertEquals(addP, "[余额支付]" + addPrice + " 元");
        org.testng.Assert.assertEquals(totalP, "[余额支付]" + (totalPrice - addPrice) + " 元");
    }

    @Then("^H验证加价重推退款记录：(\\d+)：(\\d+)$")
    public void H验证加价重推退款记录(int addPrice, int totalPrice) throws Throwable {
        Thread.sleep(1000);
        String totalP = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/ul/li[1]/span[2]")).getText();
        totalP = totalP.replaceAll("\r|\n|,|\\s*", "");//去换行，逗号，空格
        String addP = Constant.driverH.findElement(
                By.xpath("//*[@id=\"company-app\"]/div/div/div[2]/ul/li[2]/span[2]")).getText();
        addP = addP.replaceAll("\r|\n|,|\\s*", "");//去换行，逗号，空格
//        System.out.println("addP:"+addP);
//        System.out.println("totalP:"+totalP);
        org.testng.Assert.assertEquals(totalP, "【闪送账户余额】退款" + (totalPrice - addPrice) + "元成功");
        org.testng.Assert.assertEquals(addP, "【闪送账户余额】退款" + addPrice + "元成功");
    }

    @Then("^H获得优惠劵：(\\d+)：(.+)$")
    public void H获得优惠劵(int p, String type) throws Throwable {
        Thread.sleep(1000);
        String coupon = "";
        String s1 = Constant.driverH.findElements(By.cssSelector("p.c-name")).get(0).getText();
        ;
        String s2 = Constant.driverH.findElements(By.cssSelector("span.c-value")).get(0).getText();
        String s3 = Constant.driverH.findElements(By.cssSelector("span.c-world")).get(0).getText();
        if ("折扣卡".equals(type)) {
            double temp = p / 10;
            coupon = "" + temp;
            float num = (float) p / 10;
            DecimalFormat df = new DecimalFormat("0.0");
            coupon = df.format(num);
        } else {
            coupon = "" + p / 100;
        }
        Assert.assertEquals(type + coupon + s3, s1);
        Assert.assertEquals(coupon + s3, s2 + s3);
    }

    @Then("^H点击我知道了$")
    public void H点击我知道了() throws Throwable {
        if (CommonTools.isElementExist(Constant.driverH, By.xpath("//*[text()=\"我知道了\"]"))) {
            Constant.driverH.findElement(By.xpath("//*[text()=\"我知道了\"]")).click();
        }
    }
//    public static void main(String args[]){
//        int a=90;
//        double d=90/10;
//        System.out.println(d);
//    }

}
